﻿using System.Linq;
using www.Models;

namespace Data.DAO
{
    public class AdvertiseValueDAO//:BaseDAO<RELAXEntities,RelaxAdvertise>
   {
       #region value
       private readonly RelaxDataContext _context=new RelaxDataContext();
        AdvertiseRefDAO _advertiseRefDao=new AdvertiseRefDAO();
       #endregion

       #region Hàm lấy dữ liệu

       
       #endregion
       #region Add
       public void Add(ADVERTISE_VALUE advertise)
       {
           _context.ADVERTISE_VALUEs.InsertOnSubmit(advertise);
           _context.SubmitChanges();
       }
       #endregion
       #region Update

       public bool Update(ADVERTISE_VALUE advertise)
       {
           var first = _context.ADVERTISE_VALUEs.FirstOrDefault(a => a.advertiseid == advertise.advertiseid && a.languagecode==advertise.languagecode);
           if (first!=null)
           {
               first.name = advertise.name;
               first.decription = advertise.decription;
               first.image = advertise.image;
               first.link = advertise.link;
               first.target = advertise.target;
               first.Price = advertise.Price;
               first.FromDate = advertise.FromDate;
               first.ToDate = advertise.ToDate;
               first.active = advertise.active;
               _context.SubmitChanges();
               return true;
           }
           return false;
       }
       #endregion

       #region Delete
       public void Delete(long iId,long iLanguageCode)
       {
           var lData = _context.ADVERTISE_VALUEs.Where(a => a.advertiseid == iId);
           var deleteRef = lData.Count() == 1 || iLanguageCode == 0;
           if (iLanguageCode != 0)
               lData = lData.Where(p => p.languagecode == iLanguageCode);
           foreach (var item in lData)
           {
               _context.ADVERTISE_VALUEs.DeleteOnSubmit(item);
           }
           _context.SubmitChanges();
           if (deleteRef)
                _advertiseRefDao.Delete(iId);
       }
       #endregion
   }
}
